var resourceAdd = {
	resourceTypeId: null,
	flag : false,
	resourceTypeName:new Array()
};

$(function(){
	changeActiveItem('resourceList');
	getResourceType();
	savehandle();
});
/**
 * 提示信息处理
 */
function savehandle(){
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap:{resourceName:'资源名称',resourceLink:'资源链接'},
		successMsg:'添加成功',
		failmsg:'添加失败',
		success:successCallBack
	});
}
/**
 * 资源添加成功跳转
 */
function successCallBack(){
	location.href = 'resourceList.jsp';
}

/**
 * 获取数据
 */
function getResourceType(){	
	$.getJSON(
		'/fw/resource/resourceType_findAll.action',
		function(data){
			if(data&&data.rows&&data.rows.length){
	  			var rows = data.rows;
	  			var len = rows.length;
	  			var html = '';
	  			html+='<div class="rightV"><select type="text" validator="empty" class="form-control col-3p" id="resourceTypeVal" onchange="getChange(this.value)">'
	  			for(var i = 0; i < len; i++){
	  				
	  				if(rows[i].resourceTypeId)
	  					resourceTypeId = rows[i].resourceTypeId;
	  				else
	  					resourceTypeId = '暂无';
	  				
	  				if(rows[i].resourceTypeName)
	  					{
	  					 resourceTypeName = rows[i].resourceTypeName;
	  					 resourceAdd.resourceTypeName[resourceTypeId]=resourceTypeName;
	  					}
	  				else
	  					resourceTypeName = '暂无';
	  				html += '<option  value="'+resourceTypeId+'">'+resourceTypeName+'</option>';
	  			}
	  			html+='</select></div>';
	  			$('#resource').append(html);
	  		}
		}
	);
}

/**
 * 下拉框内容改变时，重新请求数据
 * @param {Object} value
 */
function getChange(value){
	resourceTypeId = value;
}
/**
 * 刷新
 */
function refresh(){
	getResourceType();
}

/**
 * 提交数据
 * @return {TypeName} 
 */
function add() {
	//验证资源链接
	var url = $('#resourceAdd .content #url').val();
	//验证资源名称
	var title = $('#resourceAdd .content #title').val();
	//验证资源类别
	var resourceTypeId = $('#resourceTypeVal').val();
	
	//验证登录用户名
	var userName = $('#resourceAdd .content #userName').val();
	//验证用户登录密码
	var password = $('#resourceAdd .content #password').val();
	
	
	//获取基本信息
	var datas = {
		'resourceType.resourceTypeId': resourceTypeId,
		url: url,
		title:title,
		"user.userName":userName,
		"user.password":password
	};

	for(var o in datas){
		if(!datas[o] && "0" != datas[o])
			delete(datas[o]);
	}
	if(!$('#resourceAddForm').validate()){
		$('#alertdiv').savehandle('setmsg',{msg:'请完善输入信息!',timer:2000});
		return false;
	}
	$.getJSON(
		'/fw/resource/resource_add.action',
		datas,
		function(data){
			$('#alertdiv').savehandle('handle',data,2000);
		}
		
	);
}

/**
 * 重写reSet函数
 */
function reSet() {
	$('#resourceType').val('');
	$('#url').val('');
	$('#title').val('');
 	$('#userName').val('');
	$('#password').val('');

}
